package com.company.admin;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

@SpringBootApplication
@ComponentScan(basePackages = "com.company.admin")
// 使用cglib的动态代理模式
@EnableTransactionManagement(proxyTargetClass = true)
// 排除和不排除效果是一样的
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.company.admin.dao")
public class AdminApplication {

	public static void main(String[] args) {
		SpringApplication.run(AdminApplication.class, args);
	}


	@Bean
	public HttpMessageConverters fastjsonMessageConvert(){
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		// 主要是为了消除循环引用
		// 如果不加，同一个对像会被解析成 $ref直接输出
		// 这是一个例子
//		public class Test {
//			public static void main(String[] args) {
//				Map<String, Student> maps = new HashMap<String, Student>();
//				Student s1 = new Student("s1", 16);
//
//				maps.put("s1", s1);
//				maps.put("s2", s1);
//
//				byte[] bytes = JSON.toJSONBytes(maps);
//
//				System.out.println(new String(bytes));
//			}
		// 输出的结果是  {"s1":{"age":16,"name":"s1"},"s2":{"$ref":"$.s1"}}

		fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
		SerializeConfig serializeConfig = new SerializeConfig();
		serializeConfig.put(Date.class,new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
		fastJsonConfig.setSerializeConfig(serializeConfig);
		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
		return new HttpMessageConverters(converter);
	}

}

